<?php


class InfoPage extends CI_Controller{
	var $_info_model, $_notice_model;
	var $per_page = 11;
	function __construct(){
		parent::__construct();
		$this->load->model("Info_Model", "", true);
		$this->load->model("Notice_Model", "", true);
		$this->load->helper('url');
		$this->load->library('session');
// 		$arr=array('id'=>'','account'=>'');
// 		$this->session->unset_userdata($arr);
		
	}
	
	function index(){
		//echo "index";
		//$this->load->view("info_index.php");
		$data["info_page"] = $this->Info_Model->query_info(5, 1)->result();
		$data["notice_page"] = $this->Notice_Model->query_notice(5, 1)->result();
		$this->load->view("Info.html", $data);
// 		$ci =& get_instance();
// 		$ci->load->helper("MY_session");
// 		if(_check_session($this))
// 			$this->load->view("info_index.php", $data);
		/*
		if($this->session->userdata('id'))
			$this->load->view("info_index.php", $data);
		else
			redirect("/login");
			*/
	}
	
	function new_info($senderID, $type, $title, $content, $time){
		$this->Info_Model->insert_info($senderID, $type, $title, $content, $time);
	}
	
	function test_new_info(){
		//echo date("Y-m-d H:i:m");
		echo $this->Info_Model->insert_info("1", 11, $_POST["title"], $_POST["content"], date("Y-m-d H:i:m"));
		
	}
	
	function test_new_notice(){
		//echo date("Y-m-d H:i:m");
		echo $this->Notice_Model->insert_notice("1", 11, $_POST["title"], $_POST["content"], date("Y-m-d H:i:m"));
	
	}
	
	function new_notice($senderID, $type, $title, $content, $time){
		$this->Notice_Model->insert_info($senderID, $type, $title, $content, $time);
	}
	
	function delete_info($ID){
		$this->Info_Model->delete_info($ID);
	}
	
	function delete_notice($ID){
		$this->Notice_Model->delete_info($ID);
	}
	
	function update_info($ID, $senderID, $type, $title, $content, $time){
		$this->Info_Model->update_info($ID, $senderID, $type, $title, $content, $time);
	}
	
	function update_notice($ID, $senderID, $type, $title, $content, $time){
		$this->Notice_Model->update_info($ID, $senderID, $type, $title, $content, $time);
	}
	
	function get_info($ID){
		$this->load->model("User_Model", "", true);
		$res = $this->Info_Model->query_info_by_ID($ID)->result();
		$sender = $this->User_Model->query_user_id($res[0]->senderID);
		$sender = $sender["Name"];
		$ret_string = sprintf("{\"title\":\"%s\",\"date\":\"%s\",\"data\":\"%s\",\"sender\":\"%s\"}", 
				$res[0]->title, $res[0]->time, $res[0]->content, $sender);
		echo $ret_string;
	}
	
	function get_notice($ID){
		$this->load->model("User_Model", "", true);
		$res = $this->Notice_Model->query_notice_by_ID($ID)->result();
		$sender = $this->User_Model->query_user_id($res[0]->senderID);
		$sender = $sender["Name"];
		$ret_string = sprintf("{\"title\":\"%s\",\"date\":\"%s\",\"data\":\"%s\",\"sender\":\"%s\"}",
				$res[0]->title, $res[0]->time, $res[0]->content, $sender);
		echo $ret_string;
	}
	
	function get_all_info(){
		return $this->Info_Model->getAll()->result();
	}
	
	function get_all_notice(){
		return $this->Notice_Model->getAll()->result();
	}
	
	function get_info_total_rows(){
		return count($this->Info_Model->getAll()->result());
	}
	
	function get_notice_total_rows(){
		return count($this->Notice_Model->getAll()->result());
	}
	
	function get_info_page($page_number=1){
		$result = $this->Info_Model->query_info($this->per_page, $page_number)->result();
		
		$this->load->library("Json");
		$hehe = new MY_Json();
		$ret = $hehe->get_page(11, $page_number, $result, $this->get_info_total_rows());
		echo $ret;
	}
	
	function get_notice_page($page_number=1){
		$result = $this->Notice_Model->query_notice($this->per_page, $page_number)->result();
	
		$this->load->library("Json");
		$hehe = new MY_Json();
		$ret = $hehe->get_page(11, $page_number, $result, $this->get_notice_total_rows());
		echo $ret;
	}
	
	function test($ID=0){
		//echo $_POST["title"];
		//echo $this->get_info($ID)->"control";
		//echo 
	}
	
}

?>